package cn.edu.tsinghua.weibo.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.tsinghua.weibo.entity.User;
import cn.edu.tsinghua.weibo.util.DB;

@SuppressWarnings("serial")
public class Follow extends HttpServlet {

	private final static String SQL = "insert into follow values(?, ?)";

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int followid = Integer.parseInt(request.getParameter("followid"));
		HttpSession session = request.getSession();
		int userid = ((User) session.getAttribute("user")).getId();

		try {
			Connection connection = DB.getConnection();
			PreparedStatement preparedStatement = connection.prepareStatement(SQL);
			preparedStatement.setInt(1, userid);
			preparedStatement.setInt(2, followid);
			preparedStatement.executeUpdate();
			DB.close(preparedStatement, connection);

//			response.sendRedirect("search.jsp");
			response.sendRedirect(request.getContextPath()+"/"+userid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
